<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
<!-- creator-valgrind.qdoc -->
  <title>Detecting Memory Leaks with Memcheck | Qt Creator Manual</title>
  <link rel="stylesheet" type="text/css" href="style/offline-simple.css" />
  <script type="text/javascript">
    document.getElementsByTagName("link").item(0).setAttribute("href", "style/offline.css");
    // loading style sheet breaks anchors that were jumped to before
    // so force jumping to anchor again
    setTimeout(function() {
        var anchor = location.hash;
        // need to jump to different anchor first (e.g. none)
        location.hash = "#";
        setTimeout(function() {
            location.hash = anchor;
        }, 0);
    }, 0);
  </script>
</head>
<body>
<div class="header" id="qtdocheader">
  <div class="main">
    <div class="main-rounded">
      <div class="navigationbar">
        <table><tr>
<td ><a href="index.html">Qt Creator Manual</a></td><td >Detecting Memory Leaks with Memcheck</td></tr></table><table class="buildversion"><tr>
<td id="buildversion" width="100%" align="right"><a href="index.html">Qt Creator Manual 4.11.1</a></td>
        </tr></table>
      </div>
    </div>
<div class="content">
<div class="line">
<div class="content mainContent">
  <link rel="prev" href="creator-valgrind-overview.html" />
  <link rel="next" href="creator-cache-profiler.html" />
<p class="naviNextPrevious headerNavi">
<a class="prevPage" href="creator-valgrind-overview.html">Using Valgrind Code Analysis Tools</a>
<span class="naviSeparator">  &#9702;  </span>
<a class="nextPage" href="creator-cache-profiler.html">Profiling Function Execution</a>
</p><p/>
<div class="sidebar">
<div class="toc">
<h3><a name="toc">Contents</a></h3>
<ul>
<li class="level1"><a href="#selecting-options-for-memory-analysis">Selecting Options for Memory Analysis</a></li>
<li class="level2"><a href="#showing-reachable-and-indirectly-lost-blocks">Showing Reachable and Indirectly Lost Blocks</a></li>
<li class="level2"><a href="#suppressing-errors">Suppressing Errors</a></li>
</ul>
</div>
<div class="sidebar-content" id="sidebar-content"></div></div>
<h1 class="title">Detecting Memory Leaks with Memcheck</h1>
<span class="subtitle"></span>
<!-- $$$creator-analyzer.html-description -->
<div class="descr"> <a name="details"></a>
<p>You can use the Memcheck tool included in the Valgrind tool suite to detect problems that are related to memory management in applications. You can use the tool together with the GDB debugger. When a problem is detected, the application is interrupted and you can debug it.</p>
<p><b>Note: </b>You can install and run Memcheck locally on Linux. You can run it on a remote host or device from any development machine. On Windows, you can use the <a href="creator-heob.html">Heob</a> heap observer to receive similar results.</p><p>After you download and install Valgrind tools, you can use Memcheck from Qt Creator.</p>
<p>To analyze applications:</p>
<ol class="1" type="1"><li>In the <b>Projects</b> mode, select a debug build configuration.</li>
<li>Select <b>Debug</b> to open the <b>Debug</b> mode, and then select <b>Memcheck</b> on the toolbar.</li>
<li>Select the <img src="images/qtcreator-analyze-start-button.png" alt="&quot;Start button&quot;" /> button to start the application.</li>
<li>Use the application to analyze it.</li>
<li>Select the <img src="images/stop_small.png" alt="&quot;Stop button&quot;" /> button to view the results of the analysis in the <b>Analysis</b> view.</li>
</ol>
<p>While the application is running, Memcheck checks all reads and writes of memory and intercepts calls that allocate or free memory or create or delete memory blocks. The results are displayed when you stop Memcheck. Click a line to view where a memory leak occurred and a stack trace that shows what caused it.</p>
<p class="centerAlign"><img src="images/qtcreator-valgrind-memcheck.png" alt="&quot;Memcheck view&quot;" /></p><p>Move the mouse on a row to view more information about the function.</p>
<p>For more information about using Memcheck, see <a href="http://valgrind.org/docs/manual/quick-start.html#quick-start.mcrun">Interpreting Memcheck's Output</a> in the Valgrind documentation.</p>
<a name="selecting-options-for-memory-analysis"></a>
<h2 id="selecting-options-for-memory-analysis">Selecting Options for Memory Analysis</h2>
<p>You can specify analyzer settings either globally for all projects or separately for each project in the <a href="creator-run-settings.html">run settings</a> of the project.</p>
<p>Stack traces can get quite large and confusing, and therefore, reading them from the bottom up can help. If the stack trace is not big enough or it is too big, select <b>Tools &gt; Options &gt; Analyzer</b> and define the length of the stack trace in the <b>Backtrace frame count</b> field.</p>
<p class="centerAlign"><img src="images/qtcreator-valgrind-memcheck-options.png" alt="&quot;Memory Analysis options&quot;" /></p><p>Memcheck also reports uses of uninitialised values, most commonly with the message <b>Conditional jump or move depends on uninitialised value(s).</b> To determine the root cause of these errors, the <b>Track origins of uninitialized memory</b> check box is selected by default. You can deselect it to make Memcheck run faster.</p>
<p>Memcheck searches for memory leaks when the client application finishes. To view the amount of leaks that occurred, select <b>Summary Only</b> in the <b>Check for leaks on finish</b> field. To also view details of each leak, select <b>Full</b>.</p>
<a name="showing-reachable-and-indirectly-lost-blocks"></a>
<h3 id="showing-reachable-and-indirectly-lost-blocks">Showing Reachable and Indirectly Lost Blocks</h3>
<p><i>Reachable</i> blocks are blocks that are pointed at by a pointer or chain of pointers and that might have been freed before the application exited. <i>Indirectly lost</i> blocks are considered lost because all the blocks that point to them are themselves lost. For example, all the children of a lost root node are indirectly lost.</p>
<p>By default, Memcheck does not report reachable and indirectly lost blocks. To have them reported, select the <b>Show reachable and indirectly lost blocks</b>.</p>
<a name="suppressing-errors"></a>
<h3 id="suppressing-errors">Suppressing Errors</h3>
<p>Memcheck detects numerous problems in the system libraries, such as the C library, which come pre-installed with your OS. As you cannot easily fix them, you want to suppress them. Valgrind reads a list of errors to suppress at startup. A default suppression file is created by the ./configure script when the system is built.</p>
<p>You can write your own suppression files if parts of your project contain errors you cannot fix and you do not want to be reminded of them. Click <b>Add</b> in the <b>Memory Analysis</b> dialog to add the suppression files. For more information about writing suppression files, see <a href="http://valgrind.org/docs/manual/manual-core.html#manual-core.suppress">Suppressing Errors</a> in the Valgrind documentation.</p>
</div>
<!-- @@@creator-analyzer.html -->
<p class="naviNextPrevious footerNavi">
<a class="prevPage" href="creator-valgrind-overview.html">Using Valgrind Code Analysis Tools</a>
<span class="naviSeparator">  &#9702;  </span>
<a class="nextPage" href="creator-cache-profiler.html">Profiling Function Execution</a>
</p>
        </div>
       </div>
   </div>
   </div>
</div>
<div class="footer">
   <p>
   <acronym title="Copyright">&copy;</acronym> 2019 The Qt Company Ltd.
   Documentation contributions included herein are the copyrights of
   their respective owners.<br>    The documentation provided herein is licensed under the terms of the    <a href="http://www.gnu.org/licenses/fdl.html">GNU Free Documentation    License version 1.3</a> as published by the Free Software Foundation.<br>    Qt and respective logos are trademarks of The Qt Company Ltd.     in Finland and/or other countries worldwide. All other trademarks are property
   of their respective owners. </p>
</div>
</body>
</html>
